在前幾天的學習中,我們已經打下了 Locust 基礎,掌握了其架構與任務管理機制。今天,讓我們將焦點轉向 Locust 的核心功能:HTTPClient。透過學習如何有效發送各種 HTTP 請求、處理不同格式的回應,以及運用 catch_response
進行自訂驗證,您將能夠建立起更全面、更貼近真實應用場景的壓力測試。
在 Locust 中,每個 HttpUser
實例都內建了一個 client
屬性,它是一個基於 Python requests
函式庫,並經過特別強化的 HTTP 客戶端。這個 client
不僅支援所有標準的 HTTP 方法,更重要的是,它會自動記錄每個請求的回應時間、成功率等關鍵效能指標,讓您能心無旁騖地專注於測試邏輯的撰寫。
無論是 GET、POST、PUT、DELETE 還是 PATCH,HTTPClient 都能輕鬆應對。以下是基本請求的程式碼範例:
from locust import HttpUser, task, between
class BasicHTTPUser(HttpUser):
wait_time = between(1, 3)
host = "http://localhost:8080"
@task
def test_get(self):
# GET 請求:從伺服器獲取資源
response = self.client.get("/products")
@task
def test_post(self):
# POST 請求:向伺服器提交資料
response = self.client.post("/cart/add", json={"item": "laptop"})
@task
def test_put(self):
# PUT 請求:更新或替換伺服器上的資源
response = self.client.put("/users/1", json={"name": "Updated Name"})
@task
def test_delete(self):
# DELETE 請求:刪除伺服器上的資源
response = self.client.delete("/items/1")
@task
def test_patch(self):
# PATCH 請求:部分更新伺服器上的資源
response = self.client.patch("/settings", json={"theme": "dark"})
GET 請求是我們最常用的 HTTP 方法。在 Locust 中,我們可以靈活地運用它來模擬各種瀏覽行為。
使用 self.client.get()
方法發送請求後,我們能輕鬆檢查 HTTP 狀態碼或解析 JSON 回應。這個功能是所有測試的基石,讓您能像真實使用者一樣瀏覽網站並驗證資料。
@task
def get_products_list(self):
"""簡單的 GET 請求範例"""
response = self.client.get("/products")
# 檢查狀態碼
if response.status_code == 200:
print("成功獲取產品列表")
# 解析 JSON 回應
data = response.json()
print(f"獲取到 {data['count']} 個產品")
當需要測試 RESTful API 時,路徑參數是不可或缺的。我們可以利用 Python 的 f-string 搭配隨機函式,來模擬使用者隨機瀏覽不同商品的行為,確保每個產品頁面都能承受壓力。
@task
def get_product_details(self):
"""使用路徑參數的 GET 請求"""
product_id = random.choice([1, 2, 3, 4, 5])
response = self.client.get(f"/products/{product_id}")
if response.status_code == 200:
product = response.json()
print(f"產品名稱: {product['name']}, 價格: ${product['price']}")
傳遞查詢參數時,除了直接寫在 URL 中,更推薦使用 params
參數。它能自動處理 URL 編碼,避免因特殊字元而產生的問題,讓程式碼更乾淨、更可靠。
@task
def search_products_method2(self):
"""使用 params 參數(推薦)"""
response = self.client.get(
"/search",
params={"q": "laptop", "category": "electronics", "sort": "price"}
)
POST 請求主要用於向伺服器提交新資料。Locust 提供了多種方式來傳送不同格式的資料,滿足各種測試需求。
現代 API 最常使用 JSON 格式。透過 json
參數,Locust 會自動設定 Content-Type: application/json
。若要發送傳統的表單資料,則使用 data
參數即可。
@task
def add_item_to_cart(self):
"""發送 JSON 格式的資料"""
item_data = {
"name": "Laptop",
"price": 1299.99,
"quantity": 1
}
response = self.client.post(
"/cart/add",
json=item_data, # 自動設定 Content-Type: application/json
headers={"Authorization": "Bearer token123"} # 可選的額外標頭
)
@task
def submit_form(self):
"""發送表單格式的資料"""
form_data = {
"username": "test_user",
"password": "password123"
}
response = self.client.post(
"/login",
data=form_data # 發送 application/x-www-form-urlencoded
)
有效的測試不僅是發出請求,更重要的是正確地解析和處理回應。
Locust 提供了多種方法來處理不同格式的回應:
response.json()
直接將 JSON 字串轉換為 Python 字典,方便存取。response.text
取得原始文字內容。response.headers
檢查如 Content-Type
或 Cache-Control
等重要資訊。catch_response
進行自訂驗證catch_response
是 Locust 提供的一個強大功能,讓您可以根據複雜的業務邏輯來判斷請求是否成功。它特別適用於以下情境:
GET /products/999
會回傳 404 狀態碼時,可以將其視為成功案例,避免影響測試結果。@task
def validate_with_catch_response(self):
"""使用 catch_response 進行自訂驗證"""
with self.client.get("/products/1", catch_response=True) as response:
if response.status_code == 200:
product = response.json()
# 自訂驗證邏輯
if product["price"] > 0 and product["stock"] > 0:
response.success()
else:
response.failure("產品價格或庫存異常")
在真實世界的測試中,我們常常需要用前一個請求的回應資料來執行後續請求,這就是所謂的「連鎖請求」。這個技巧能完美模擬使用者從搜尋、瀏覽到最終購買的完整操作流程,大幅提升測試的擬真度。
@task
def chain_requests_with_data(self):
"""使用提取的資料進行連鎖請求"""
# 步驟 1: 搜尋產品,從回應中獲取產品 ID
search_response = self.client.get("/search?q=mouse")
if search_response.status_code == 200:
products = search_response.json()["products"]
if products:
product_id = products[0]["id"]
# 步驟 2: 使用產品 ID 獲取詳情
detail_response = self.client.get(f"/products/{product_id}")
if detail_response.status_code == 200:
# 步驟 3: 將產品加入購物車
cart_response = self.client.post("/cart/add", json=cart_item)
if cart_response.status_code == 200:
print(f"完成購買流程: 搜尋 -> 查看 -> 加入購物車")
catch_response
:它會帶來一些額外開銷,只在需要複雜驗證時才使用。今天的學習讓我們對 Locust HTTPClient 有了全面的認識。我們學會了如何:
catch_response
進行強大的自訂驗證。這些技能不僅能讓您測試伺服器的效能,更能確保業務邏輯在壓力下的正確性。
明天,我們將繼續探索如何使用 Cookies 管理 Session、處理認證機制,以及實作更複雜的使用者會話管理。這些進階技巧將讓您的 Locust 測試更上一層樓,敬請期待!